/**
 * 
 */
package com.jeecms.wicool.action.shops;

import static com.jeecms.common.page.SimplePage.cpn;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.shops.Adviser;
import com.jeecms.wicool.manager.shops.AdviserMng;

/**
 * @author spring
 *
 */
@Controller
public class AdviserAct {
	private static final Logger log = LoggerFactory.getLogger(AdviserAct.class);
	
	@Autowired
	private AdviserMng adviserManager;
	
	@RequestMapping("/adviser/v_listAdviser.do")
	public String list(String name,String loginName,
			Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = adviserManager.getPage(name, loginName, cpn(pageNo), CookieUtils.getPageSize(request),CmsUtils.getUser(request));
		model.addAttribute("pagination", pagination);
		model.addAttribute("name", name);
		model.addAttribute("loginName", loginName);
		return "shops/list_Adviser";
	}
	
	@RequestMapping("/adviser/v_addadviser.do")
	public String add(ModelMap model,HttpServletRequest request) {
		Adviser adviser= new Adviser();
		//List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("adviser", adviser);
		//model.addAttribute("listShop", listShop);
		return "shops/add_Adviser";
	}
	
	@RequestMapping("/adviser/o_saveAdviser.do")
	public String save(Adviser bean, Integer pageNo,HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = adviserManager.save(bean);
		log.info("save Adviser id={}", bean.getId());
		return "redirect:v_listAdviser.do";
	}
	
	@RequestMapping("/adviser/v_editAdviser.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(id, errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		Adviser adviser = adviserManager.findById(id);
		//List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("adviser", adviser);
		//model.addAttribute("listShop", listShop);
		return "shops/edit_Adviser";
	}
	
	@RequestMapping("/adviser/o_updateAdviser.do")
	public String update(Adviser bean, String name,String loginName, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors = WebErrors.create(request);
		vldExist(bean.getId(), errors);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = adviserManager.update(bean);
		model.addAttribute("message", "global.success");
		log.info("update Adviser success. id={}", bean.getId());
		return list(name,loginName, pageNo, request, model);
	}
	
	@RequestMapping("/adviser/o_deleteAdviser.do")
	public String delete(Integer[] ids, String name,String loginName, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		WebErrors errors =  WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		Adviser[] beans = adviserManager.deleteByIds(ids);
		for (Adviser bean : beans) {
			log.info("delete Adviser id={}", bean.getId());
		}
		return list(name, loginName, pageNo, request, model);
	}
	
	
	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		Adviser entity = adviserManager.findById(id);
		if (errors.ifNotExist(entity, Adviser.class, id)) {
			return true;
		}
		return false;
	}
	
}
